package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;

public class CriNewsAnalyse implements AnalyseNews {

	
	public boolean isDetailPage(String url) {
		//http://gb.cri.cn/27824/2012/11/22/2625s3933953.htm
		boolean bRet=false;
		String[] regex={
				"http://.*[.]cri[.]cn/[0-9]*/[0-9]{4}/[0-9]{2}/[0-9]{2}/[0-9]*[a-z]{1}[0-9]*[.]htm"};
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				bRet=true;
				break;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		String html=urlMeta.getHtml();
		String url=urlMeta.getUrl();
		String title="";
		String content="";;
		Long date=null;
		int commentNum=0;
		int clickNum=0;
		NewsMeta meta=new NewsMeta();
		meta.setUrl(urlMeta.getUrl());
		meta.setType(0);
		Document doc=Jsoup.parse(html);
		urlMeta=null;
		/*
		 * 解析新闻标题
		 */
		title=doc.select("h1#ctitle").text();
		if(StringUtils.isBlank(title)){
			title=doc.select("h3#ctitle").text();
			if(StringUtils.isBlank(title)){
				title=doc.select("title").text();
				title=StringUtils.substringBefore(title, "-");
				if(StringUtils.isBlank(title)){
					title=doc.select("div.content01").text();
				}
			}
		}


		/*
		 * 解析时间
		 */
		date=DateUtils.matchDate(doc.select("div.signdate").text());
		if(date==null){
			date=DateUtils.matchDate(doc.select("div.ly_edu").text());
		}
		if (date== null) {
			date=DateUtils.matchDate(doc.select("div.news_ts_ly").text());
		}
		if (date== null) {
			date=DateUtils.matchDate(doc.select("div.content02").text());
		}
		if (date==null) {
			date=JSoupUtils.matchDate(doc, "来源");
		}

		/*
		 * 解析内容
		 */
		content=HtmlCleaner.getContentHtml(url,doc.select("div#ccontent"));

		meta.setTitle(title);
		meta.setContent(content);
		meta.setDate(date);
		meta.setClickNum(clickNum);
		meta.setCommentNum(commentNum);

		String author=doc.select("div.warp div.news_ts_l div.news_ts_ly a").text();
		if (StringUtils.isBlank(author)){
			author=JSoupUtils.matchAuthor(doc, "编辑：");
		}
		meta.setAuthor(author);
		return meta;
	}

	public static void main(String[] args) {
		String url="http://big5.cri.cn/gate/big5/gb.cri.cn/42071/2014/08/07/882s4644121.htm";
		UrlMeta urlMeta=CrawlHTML.responseToURL(url);
		CriNewsAnalyse cnhubeiNewsAnalyse=new CriNewsAnalyse();
		boolean detailPage=cnhubeiNewsAnalyse.isDetailPage(url);
		if(detailPage){
			NewsMeta parserHtml =cnhubeiNewsAnalyse.parserHtml(urlMeta);
			System.out.println(parserHtml);
		}else{
			System.out.println("不符合正则");
		}
	}

	
	public NewsMeta Update(NewsMeta meta) {
		/**
		 * 没有评论数信息
		 */
		return null;
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}
